<template>
  <div class="mycon">
    <!-- 已登录状态 -->
    <div class="header user-info" v-if="userTok">
      <div class="base-info header">
        <div class="left">
          <van-image class="avatar" src="https://img01.yzcdn.cn/vant/cat.jpeg" fit="cover" round />
          <span class="name">{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button size="mini" round>编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count"></span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count"></span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count"></span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count"></span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>

    <!-- 未登录状态 -->
    <div class="header not_login" v-else>
      <div class="login_box" @click="$router.push({path: '/login'})">
        <img src="~@/assets/mobile.png" />
        <span class="text">登录 / 注册</span>
      </div>
    </div>

    <!-- 收藏导航 -->
    <van-grid :column-num="2" border clickable>
      <van-grid-item>
        <template #icon>
          <i class="iconfont myIcon-shoucang"></i>
        </template>
        <template #text>收藏</template>
      </van-grid-item>
      <van-grid-item>
        <template #icon>
          <i class="iconfont myIcon-lishi"></i>
        </template>
        <template #text>历史</template>
      </van-grid-item>
    </van-grid>
    <!-- 单元格 -->
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell border @click="loginOutFn" class="loginOutBtn" value="退出登录" v-if="userTok" />
  </div>
</template>

<script>
// 将vuex数据仓库内的数据映射到当前组件
import { mapState } from 'vuex'
// 导入请求用户基本信息API
import { getUserInfoAPI } from '@/api/user.js'
export default {
  data() {
    return {
      userInfo: null
    }
  },
  computed: {
    ...mapState(['userTok'])
  },
  methods: {
    // 获取用户基本信息方法
    async getUserInfo() {
      try {
        const { data } = await getUserInfoAPI()
        console.log(data)
        this.userInfo = data.data
        this.$toast.fail('获取用户基本信息成功!')
      } catch (error) {
        this.$toast.fail('获取用户基本信息失败')
      }
    },
    // 退出登录
    loginOutFn() {
      this.$dialog
        .confirm({
          title: '真要退出去?'
        })
        .then(() => {
          // 点击确认退出后会掉用vuex数据仓库中的初始化数据方法并传入空对象让数据重新初始化
          this.$store.commit('setUserTok', null)
        })
        .catch(() => {
          console.log('取消退出')
        })
    }
  },
  created() {
    this.getUserInfo()
  }
}
</script>

<style scoped lang="less">
.mycon {
  .header {
    height: 361px;
    background-image: url('~@/assets/banner.png');
    background-size: cover;
  }

  .not_login {
    // 未登录样式
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;

    .login_box {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      img {
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 28px;
        color: #ffffff;
      }
    }
  }
  // 登录后样式
  .user-info {
    .base-info {
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          border: 4px solid #fff;
          margin-right: 23px;
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
    }
    .data-stats {
      display: flex;
      .data-item {
        height: 130px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  .myIcon-shoucang {
    color: red;
  }

  .loginOutBtn .van-cell__value--alone {
    text-align: center;
    color: red;
    font-weight: 700;
  }
}
</style>
